namespace EntLibEC.Shopping.Include
{
	using System;
	using System.Data;
	using System.Drawing;
	using System.Web;
	using System.Web.UI;
	using System.Web.UI.WebControls;
	using System.Web.UI.HtmlControls;

	using EntLibEC.Info.Online;
	using EntLibEC.Info.Basic;
	using EntLibEC.Cmn;
	using EntLibEC.Biz;

	/// <summary>
	///		Summary description for BrowsedProductList.
	/// </summary>
	public partial class BrowsedProductList : System.Web.UI.UserControl
	{
		protected void Page_Load(object sender, System.EventArgs e)
		{				
			SetSessionInfoBrowsedProduct();
			LoadData();
		}

		private int productCout = 0 ;

		public void LoadData()
		{
			EntLibSessionInfo oSession = GetSession();
			if (oSession.BrowsedProduct == null || oSession.BrowsedProduct.Rows.Count == 0)
			{
				tabBrowsedProduct.Visible = false;
				return;
			}

			tabBrowsedProduct.Visible = true;
			productCout = oSession.BrowsedProduct.Rows.Count;
			rptBrowsedProduct.DataSource = oSession.BrowsedProduct;
			rptBrowsedProduct.DataBind();
		}

		protected void SetSessionInfoBrowsedProduct()
		{		
			string sysNos = CookieUtil.GetCookie(AppConst.COOKIE_BROWSED_PRODUCT);
			if (GetSession().BrowsedProduct == null)
			{				
				if (sysNos != null)
				{
					if (Util.IsValidNumber(sysNos.Replace("+", "")))
					{
						GetSession().BrowsedProduct = Biz.Basic.ProductManager.GetProductShortInfoBySysNo(sysNos.Replace("+", ","));
					}
					else
					{
						CookieUtil.SetCookie(AppConst.COOKIE_BROWSED_PRODUCT, "", new DateTime(1900, 1, 1));
						GetSession().BrowsedProduct = new DataTable();
					}
				}
			}	
			else
			{
				if (sysNos == null)
				{
					GetSession().BrowsedProduct = new DataTable();
				}
			}
		}

		protected EntLibSessionInfo GetSession()
		{
			EntLibSessionInfo oSession = (EntLibSessionInfo)Session["EntLibSessionInfo"];
			if ( oSession == null )
			{
				oSession = new EntLibSessionInfo();
				Session["EntLibSessionInfo"] = oSession;
			}
			return oSession;
		}

        protected void btnClear_ServerClick(object sender, EventArgs e)
        {
            CookieUtil.SetCookie(AppConst.COOKIE_BROWSED_PRODUCT, "", new DateTime(1900, 1, 1));
            EntLibSessionInfo oSession = (EntLibSessionInfo)Session["EntLibSessionInfo"];
            if (oSession != null && oSession.BrowsedProduct != null)
            {
                oSession.BrowsedProduct = null;
                Session["EntLibSessionInfo"] = oSession;
            }
            tabBrowsedProduct.Visible = false;
        }


        protected void rptBrowsedProduct_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                System.Web.UI.WebControls.Image imgControl = (System.Web.UI.WebControls.Image)e.Item.FindControl("ImgProduct");
                imgControl.ImageUrl = ProductBasicInfo.GetSmallPic(AppConfig.PicturePath, Convert.ToString(DataBinder.Eval(e.Item.DataItem, "ProductID")));
                imgControl.ToolTip = Util.RemoveHtmlTag(Convert.ToString(DataBinder.Eval(e.Item.DataItem, "BriefName")));

            }
        }

//		private void igbClear_Click(object sender, System.Web.UI.ImageClickEventArgs e)
//		{
//			CookieUtil.SetCookie(AppConst.COOKIE_BROWSED_PRODUCT, "", new DateTime(1900, 1, 1));
//			EntLibSessionInfo oSession = (EntLibSessionInfo)Session["EntLibSessionInfo"];
//			if ( oSession != null && oSession.BrowsedProduct != null)
//			{
//				oSession.BrowsedProduct = null;
//				Session["EntLibSessionInfo"] = oSession;
//			}
//			tabBrowsedProduct.Visible = false;
//		}
	}
}
